package com.example.demo.strategy.context;

import com.example.demo.strategy.UploadStrategy;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import static com.example.demo.Enums.UploadStrategyEnum.getStrategy;

import java.io.FileInputStream;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class UploadStrategyContext {

    @Value("${upload.model}")
    private String model;

    private final Map<String, UploadStrategy> uploadStrategyMap;


    /**
     * Execute upload strategy string.
     * 执行策略
     *
     * @param path the path
     * @param file the file
     * @return the string
     */
    public String executeUploadStrategy(String path, MultipartFile file) {
        return uploadStrategyMap.get(getStrategy(model)).upload(path, file);
    }


    /**
     * Execute upload strategy string.
     * 执行策略
     *
     * @param name        the name
     * @param path        the path
     * @param inputStream the input stream
     * @return the string
     */
    public String executeUploadStrategy(String name, String path, FileInputStream inputStream) {
        return uploadStrategyMap.get(getStrategy(model)).upload(name,path,inputStream);
    }
}

